package com.polidea.rxohosble2.sample.example1_scanning;


import com.polidea.rxohosble2.RxBleDevice;
import com.polidea.rxohosble2.sample.ResourceTable;
import com.polidea.rxohosble2.scan.ScanResult;
import ohos.agp.components.*;

import java.util.*;

public class ScanResultsAdapter extends BaseItemProvider {

    public ScanResultsAdapter(OnClickListener listener) {
    this.listener = listener;
    }

    interface OnAdapterItemClickListener {
        void onAdapterViewClick(Component component);
    }
    private static final Comparator<ScanResult> SORTING_COMPARATOR =(lhs, rhs) ->
            lhs.getBleDevice().getMacAddress().compareTo(rhs.getBleDevice().getMacAddress());

    private OnAdapterItemClickListener onAdapterItemClickListener;
    private final Component.ClickedListener onClickListener = new Component.ClickedListener(){
        @Override
        public void onClick(Component c) {

            if (onAdapterItemClickListener != null) {
                onAdapterItemClickListener.onAdapterViewClick(c);
            }
        }
    };

    void addScanResult(ScanResult bleScanResult) {
        // Not the best way to ensure distinct devices, just for sake on the demo.
        for (int i = 0; i < data.size(); i++) {

            if (data.get(i).getBleDevice().equals(bleScanResult.getBleDevice())) {
                data.set(i, bleScanResult);
                notifyDataSetItemChanged(i);
                return;
            }
        }

        data.add(bleScanResult);
        Collections.sort(data, SORTING_COMPARATOR);
        notifyDataChanged();
    }

    void clearScanResults() {
        data.clear();
        notifyDataChanged();
    }

    ScanResult getItemAtPosition(int childAdapterPosition) {
        return data.get(childAdapterPosition);
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        if (position > 0 && position < data.size()){
            return data.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    private final List<ScanResult> data = new ArrayList<>();

    @Override
    public Component getComponent(int position, Component convertComponent, ComponentContainer componentContainer) {
        final Component cpt;
        if (convertComponent == null) {
            cpt = LayoutScatter.getInstance(componentContainer.getContext()).parse(ResourceTable.Layout_item_discovery_service, componentContainer, false);
        } else {
            cpt = convertComponent;
        }
        final ScanResult rxBleScanResult = data.get(position);
        final RxBleDevice bleDevice = rxBleScanResult.getBleDevice();
        Text textTone = (Text) cpt.findComponentById(ResourceTable.Id_text1);
        Text textTwo = (Text) cpt.findComponentById(ResourceTable.Id_text2);
        textTone.setText(String.format(Locale.getDefault(), "%s (%s)", bleDevice.getMacAddress(), bleDevice.getName()));
        textTwo.setText(String.format(Locale.getDefault(), "RSSI: %d", rxBleScanResult.getRssi()));
        cpt.setClickedListener(component -> {
            if (this.listener != null){
                listener.onclick(position);
            }
        });
        return cpt;
    }

    private OnClickListener listener;

    public interface OnClickListener{

        void onclick(int position);

    }
}